﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public enum VoucherStatus { Valid = 1, Expired = 2, Invalid = 3, Used = 4 }
    public enum PaymentTypes { Credit = 1, Voucher = 2 }

    public partial class Booking
    {
        private bool isValid = true;
        private string errorMessage = string.Empty;

        public bool IsValid
        {
            get { return isValid; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
        }

        public Guest Guest
        {
            get { return Guests.FirstOrDefault(); }
        }

        public void GenerateBookingNumber()
        {
            BookingNumber = "BID" + DateTime.Now.ToString("yyyyMMdd") + new Random().Next(1000, 9999);
        }

        private void SetError(string message)
        {
            isValid = false;
            errorMessage += message + Environment.NewLine;
        }

        public bool MakeBooking(Booking  booking)
        {
            // Check booking start date and end date
            if (booking.StartDate.HasValue && booking.EndDate.HasValue && booking.StartDate.Value.CompareTo(booking.EndDate.Value) != -1)
            {
                SetError("Invalid start date & end date.");
            }
            else
            {
                this.StartDate = booking.StartDate;
                this.EndDate = booking.EndDate;
            }

            // Check number of room
            if (booking.NoOfRoom < 1)
            {
                SetError("Invalid number of room.");
            }
            else
            {
                this.NoOfRoom = booking.NoOfRoom;
            }

            // Check number of nights
            if (booking.RoomTypeID != null)
            {
                this.RoomTypeID = booking.RoomTypeID;
            }

            if (isValid)
            {
                GenerateBookingNumber();
                this.BookingStatus = "Confirmed";
            }

            return isValid;
        }
    }
}
